<?php

namespace App\Http\Controllers;

use App\Models\Plan;
use App\Models\Shop;
use App\Models\User;
use App\Models\XunDianJiLu;
use Illuminate\Http\Request;

class ReportController extends Controller
{
    /**
     * 门店维度报表
     *
     * @author Eric
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function shop(Request $request)
    {
        $params = $this->validate($request, [
            'name' => 'nullable',
            'code' => 'nullable',
        ]);

        // 分页数
        $paginate = $request->get('paginate', 10);

        // 时间范围
        $start_time = date('Y-m-01 00:00:00');

//        $time =

        if (count($request->created_at) == 0 || $request->created_at == '')
        {
            $created_time = [
                $start_time,
                date('Y-m-d 23:59:59', strtotime(" $start_time +1 month -1 day"))
            ];
        } else {
            $created_time = $request->created_at;
        }

        $query = Shop::query();

        $query->with(['xunDianJiLus' => function($query) use ($created_time){
            $query->whereBetween('created_at', $created_time);
        }]);


        foreach ($params as $key => $value) {
            if ($value) {
                $query->where($key, 'like', "%".$value."%");
            }
        }

        $data = $query->orderBy('created_at','desc')->paginate($paginate);


        // 巡店数据合计
        foreach ($data as $k => $v){
            $v->xun_dian_ji_lus_num = count($v->xunDianJiLus);
        }

        return $this->success($data);
    }

    /**
     * 根据[门店id, 用户id]查询巡店记录
     *
     * @author Eric
     * @param $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function shop_xun_dian_ji_lu(Request $request)
    {

        $params = $this->validate($request, [
            'shop_id' => 'nullable',
            'shop_name' => 'nullable',
            'uid'  => 'nullable',
        ]);
        // 分页数
        $paginate = $request->get('paginate', 10);

        $query = XunDianJiLu::query();

        // 时间范围
        $start_time = date('Y-m-01 00:00:00');

        if (count($request->created_at) == 0 || $request->created_at == '')
        {
            $created_time = [
                $start_time,
                date('Y-m-d 23:59:59', strtotime(" $start_time +1 month -1 day"))
            ];
        } else {
            $created_time = $request->created_at;
        }

        $query->whereBetween('created_at', $created_time);

        foreach ($params as $key => $value) {
            if ($value) {
                $query->where($key, 'like', "%".$value."%");
            }
        }

        $data = $query->orderBy('created_at','desc')->paginate($paginate);


        return $this->success($data);
    }

    /**
     * 人员维度报表
     *
     * @author Eric
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function user(Request $request)
    {
        $params = $this->validate($request, [
            'name' => 'nullable',
            'staffid' => 'nullable',
        ]);

        // 分页数
        $paginate = $request->get('paginate', 10);

        // 时间范围
        $start_time = date('Y-m-01 00:00:00');

        if (count($request->created_at) == 0 || $request->created_at == '')
        {
            $created_time = [
                $start_time,
                date('Y-m-d 23:59:59', strtotime(" $start_time +1 month -1 day"))
            ];
        } else {
            $created_time = $request->created_at;
        }

        $query = User::query();

        $query->with(['xunDianJiLus' => function($query) use ($created_time){
            $query->whereBetween('created_at', $created_time);
        }]);


        foreach ($params as $key => $value) {
            if ($value) {
                $query->where($key, 'like', "%".$value."%");
            }
        }

        $data = $query->orderBy('created_at','desc')->paginate($paginate);


        // 巡店数据合计
        foreach ($data as $k => $v){
            $v->xun_dian_ji_lus_num = count($v->xunDianJiLus);
        }

        return $this->success($data);
    }

    /**
     * 任务维度报表
     *
     * @author Eric
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function plan(Request $request)
    {

        // 分页数
        $paginate = $request->get('paginate', 10);

        // 时间范围
        $start_time = date('Y-m-01 00:00:00');

        if (count($request->created_at) == 0 || $request->created_at == '')
        {
            $created_time = [
                $start_time,
                date('Y-m-d 23:59:59', strtotime(" $start_time +1 month -1 day"))
            ];
        } else {
            $created_time = $request->created_at;
        }

        $query = Plan::query();

        $query->whereBetween('created_at', $created_time);

        $query->with(['shop', 'XunDianJiLu', 'user']);

        if($request->status === 0 || $request->status === 1){
            $query->where('status', $request->status);
        }

        $data = $query->orderBy('created_at','desc')->paginate($paginate);


        // 巡店数据合计
        foreach ($data as $k => $v)
        {
            if($v->status == 0)
            {
                $v->status_str = '未完成';
            } else {
                $v->status_str = '已完成';
            }

        }

        return $this->success($data);
    }
}
